import java.awt.Graphics;

import javax.swing.JPanel;


public class Player extends JPanel
{
	static final int MAX_DICE = 5;
	
	// Player states
	static final int PLAYER_IDLE  = 0;
	static final int PLAYER_ROLL  = 1;
	static final int PLAYER_SCORE = 2;
	
	/*--- Member Variables ---*/
	// grand total score of the game
	int totalScore;
	// player number
	int index;
	int pNum;
	// current state
	int pState;
	// current score card clicked
	int scoreClick;

	/*--- Class Variables ---*/
	Dice[] d;
	Scoreboard sb;

	Player()
	{
		d = new Dice[MAX_DICE];
		sb = new Scoreboard();
		
		for (int i = 0; i < d.length; i++) {
			d[i] = new Dice();
			d[i].setIndex(0);
			d[i].setPosition(i*50, 50);
		}
	}
	
	void draw(Graphics g)
	{
		for (int i = 0; i < d.length; i++) {
			d[i].paintComponent(g);
		}
		sb.draw(g);
	}
	
	void update(Coordinates a_position, Coordinates a_screen)
	{
		switch(pState)
		{
		case PLAYER_IDLE:
			break;
		case PLAYER_ROLL:
			diceRollAll();
			break;
		case PLAYER_SCORE:
			sb.update(d, a_position, a_screen);
			break;
		}
		for (int i = 0; i < d.length; i++) {
		}
	}
	
	/*--- Main Game Functions ---*/
	boolean allFrozen()
	{
		int t_check = 0;

		// checks for how many dice are frozen
		for(int i = 0; i < MAX_DICE; i++){
			if(d[i].isFrozen()){ t_check++; }
		}

		// if all dice are return true if not return false
		if(t_check == MAX_DICE){ return true;}
		return false;
	}
	boolean allScored()
	{
		int t_score = 0;
		for(int i = 0; i < sb.getItems(); i++){
			if(sb.isScored(i)) t_score++;
		}
		if(t_score == sb.getItems()) return true;
		else return false;
	}
	//dice roll all
	void diceRollAll()
	{
		for(int i = 0; i < MAX_DICE; i++){
			if(!d[i].isFrozen()){
				d[i].Roll();
			}
		}
	}
	//game stop on the dice
	void freezeDiceValue(int a_index)
	{
		if(!d[a_index].isFrozen()){
			d[a_index].setFreeze(true);
		}
		else { d[a_index].setFreeze(false); }
	}

	boolean isScoreClicked(Coordinates a_position, int a_w)
	{
		if((a_position.getY() > 0 && a_position.getY() < (sb.getHieght() * sb.getItems())) &&
			(a_position.getX() > (a_w - sb.getWidth()) && a_position.getX() < a_w)){
				return true;
		}
		return false;
	}
	//score keeping for the round
	boolean roundScored(int a_gRound)
	{
		int t_score = 0;
		for(int i = 0; i < sb.getItems(); i++)
			if(sb.isScored(i)) t_score++;
		if(t_score > a_gRound) return true;
		else return false;
	}
	//unfreeze the all dice 
	void unFreezeAll()
	{
		for(int i = 0; i < MAX_DICE; i++){
			if(d[i].isFrozen()) d[i].setFreeze(false);
		}
	}
	/*--- Accessors/Mutators ---*/
	// these are set in the initialization function once player is created
	void setIndex(int a_index) { index = a_index; }
	void setGameState(int a_state) { pState = a_state; }
	void setScoreClick(int a_index) { scoreClick = a_index; }
	int getDicePosX(int a_index) {return d[a_index].getX();}
	int getDicePosY(int a_index) {return d[a_index].getY();}
	int getDiceDim(int a_index) {return d[a_index].getDimension();}
}
